/*
 * Copyright (c) 2016, www.zm0618.com All Rights Reserved..
 */

package com.ibetter.mr.grade.medic.tag;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.io.IOException;

/**
 * Created by zhaojun on 2016/12/19.
 */
public class GradeUserTagScoreMapper extends Mapper<LongWritable,Text,LongWritable,Text>{
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        FileSplit fileSpit = (FileSplit) context.getInputSplit();
        Path path = fileSpit.getPath();
        String currentPath=path.toString();
        String[] pathSplit=currentPath.split("/");
        if (StringUtils.equals(pathSplit[pathSplit.length-2],"tag_rel")){
            if (value!=null){
                String  tagRelStr= value.toString();
                if (StringUtils.isNotBlank(StringUtils.trim(tagRelStr))){
                    String[] tagRels = tagRelStr.split(",");
                    Text tx=new Text(StringUtils.join(new String[]{"r",StringUtils.trim(tagRels[2])},":"));
                    context.write(new LongWritable(Long.parseLong(StringUtils.trim(tagRels[1]))),tx);
                }
            }
        }

        if (StringUtils.equals(pathSplit[pathSplit.length-3],"output")){
            if (value!=null) {
                String scoreStr = value.toString();
                if (StringUtils.isNotBlank(StringUtils.trim(scoreStr))){
                    String[] scoreStrs = scoreStr.split("\t");
                    Text tx=new Text(StringUtils.join(new String[]{"s",StringUtils.trim(scoreStrs[0]),StringUtils.trim(scoreStrs[2])},":"));
                    context.write(new LongWritable(Long.parseLong(StringUtils.trim(scoreStrs[1]))),tx);
                }
            }
        }

    }
}
